﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Presentation.Pages
{
    public partial class NoticiasCadastro : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!Util.usuarioEhAdmin(this.Context))
                    Response.Redirect("~/Pages/index.aspx");
                else
                    carregarNoticias();
            }
        }

        private void carregarNoticias()
        {
            try
            {
                using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                {
                    List<DENoticia> noticias = banco.DENoticias.OrderByDescending(q => q.Data).ToList();

                    grvNoticias.DataSource = noticias;
                    grvNoticias.DataBind();
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Erro ao carregar notícias");
            }
        }

        protected void btnAtivarDesativar_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Util.usuarioEhAdmin(this.Context))
                    Response.Redirect("~/Pages/index.aspx");
                else
                {
                    Button btn = sender as Button;
                    Int32 id = Convert.ToInt32(btn.CommandArgument);

                    using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                    {
                        DENoticia noticia = banco.DENoticias.Single(q => q.Codigo.Equals(id));
                        noticia.Ativo = !noticia.Ativo;

                        banco.SaveChanges();
                    }

                    carregarNoticias();

                    Util.showMessage(this, "Alterado com sucesso");
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Erro ao alterar notícia");
            }
        }

        protected void btnAdicionarFoto_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Util.usuarioEhAdmin(this.Context))
                    Response.Redirect("~/Pages/index.aspx");
                else
                {
                    Button btn = sender as Button;
                    Int32 id = Convert.ToInt32(btn.CommandArgument);

                    Response.Redirect("~/Pages/NoticiasCadastroFotos.aspx?codigo=" + id);
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Erro ao alterar notícia");
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                if (Util.usuarioEhAdmin(this.Context))
                {
                    DENoticia noticia = new DENoticia();
                    noticia.Titulo = txtTitulo.Text;
                    noticia.Data = Convert.ToDateTime(this.txtData.Text);
                    noticia.Corpo = this.txtCorpoHTML.Text.Replace("__", "<").Replace("--", ">");
                    noticia.CorpoHTML = noticia.Corpo;
                    noticia.Ativo = false;
                    noticia.Padrao = true;

                    using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                    {
                        banco.AddToDENoticias(noticia);
                        banco.SaveChanges();
                    }

                    carregarNoticias();

                    this.txtCorpoHTML.Text = String.Empty;
                    this.txtData.Text = String.Empty;
                    this.txtTitulo.Text = String.Empty;

                    Util.showMessage(this, "Notícia cadastrado com sucesso");
                }
                else
                    Response.Redirect("~/Pages/index.aspx");
            }
            catch (Exception)
            {
                Util.showMessage(this, "Houve um erro ao cadastrar uma nova notícia");
            }
        }
    }
}